package com.tvunetworks.center.device.controller.openapi;

import com.tvunetworks.center.common.model.PreloadParam;
import com.tvunetworks.center.common.model.param.PeerIdParam;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.device.model.MyDevice;
import com.tvunetworks.center.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @program: tvucc-aggregator
 * @description: DeviceMatrixFeignController
 * @author lebronchen
 * @create: 2020-06-02 16:44
 **/
@Slf4j
@RestController
@RequestMapping(value = "uniformapi/${openApiVersion}/device")
public class DeviceUniformApiController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 获取 R 下发 Ext Grid 的 preload 信息
     * @param param param
     * @return Result
     */
    @PostMapping("/getRPreloadMessage")
    public Result getRPreloadMessage(@RequestBody @Valid PeerIdParam param){
        PreloadParam result = deviceService.getRPreloadMessage(param.getPeerId().toLowerCase());
        return ResultUtil.success(result);
    }

    /**
     *
     * @param param param
     * @return Result
     */
    @PostMapping("/getMetadata")
    public Result getMetadata(@RequestBody @Valid PeerIdParam param){
        MyDevice dbDevice = deviceService.getDBDevice(param.getPeerId());
        if (dbDevice == null){
            return ResultUtil.deviceNotExist();
        }
        return ResultUtil.success(dbDevice.getMetaData() != null? dbDevice.getMetaData():"");
    }

}
